<?php

class Public_IndexSuccessView extends BlogPublicBaseView
{
	/**
	 * Presentational logic.
	 *
	 * @param AgaviRequestDataHolder $rd
	 */
	public function executeHtml(AgaviRequestDataHolder $rd)
	{
        // load the default rendering configuration for HTML
        $this->setupHtml($rd, 'blog');
        $params = $rd->getParameters();
        /* @var $model Public_CommentsModel */
        $comments_model = $this->context->getModel('Comments', 'Public');
        
        $ro = $this->getContext()->getRouting();
        
        // $start is a number of post we should start from
        $start = 0;
        if (isset($params['start'])) {
            $start = $params['start'];
        } 
        
        $posts_per_page = WereWordConfig::get('blog.index_posts_count');
        
        $posts = $this->findPosts();
        $posts_count = $posts->count();
        
        $entries = array();
       
        for ($i=$start; $i<$posts_per_page*($start+1); $i++) {
            if ($i >= $posts_count) {
                break;
            }
            $post = $posts->get($i);
            // extract post time components
            $dt = new DateTime($post->post_time);
            $Y = $dt->format('Y');
            $M = $dt->format('m');
            $D = $dt->format('d');

            // $Y, $M, $D
            $shortcut = $post->shortcut;
            $route_elements = array('year'=>$Y, 'month'=>$M, 'day'=>$D, 'shortcut'=>$shortcut);

            // read post tags
            $tag_strings = array();
            foreach ($post->Tags as $tag) {
                $tag_strings[] = $tag->tag;
            }
            $entry = array('title' => MarkupProcessor::getPostTitleHtml($post),
                'body' => MarkupProcessor::getPostSummaryHtml($post),
                'read_more' => strpos($post->Body->body, MarkupProcessor::SUMMARY_DELIMITER) !== FALSE,
                'D' => $D,
                'M' => $M,
                'Y' => $Y,
                'time' => $dt->format('H:i'),
                'shortcut' => $shortcut,
                'show_route' => $ro->gen('post.view', $route_elements),
                'edit_route' => $ro->gen('post_by_id.edit', array('post_id'=>$post->id)),
                'delete_route' => $ro->gen('post_by_id.delete', array('post_id'=>$post->id)),
                'tags' => $tag_strings,
                'comments' => $comments_model->getBlogPostCommentsCount($post, FALSE),
                'comments_allowed' => ($post->is_commenting_enabled),
            	'published' => ($post->is_published),
            );
            
            $entries[] = $entry;
        }
        
        // calculate page number (starts from `0')
        $page = (int)(($start) / $posts_per_page);
        $max_page = (int)($posts_count / $posts_per_page)-1;
        if ($posts_count % $posts_per_page != 0) {
            $max_page++;
        }
        if ($page === 1) { // i.e. second page
            $this->setAttribute('newer_url', $ro->gen('root'));
        } elseif ($page > 0) { 
            $this->setAttribute('newer_url', $ro->gen('root', array('start'=>$posts_per_page*($page-1))));
        }
        
        if ($page < $max_page) {
            $this->setAttribute('older_url', $ro->gen('root', array('start'=>$posts_per_page*($page+1))));
        }
        $this->setAttribute('rss', $ro->gen('feeds.blog'));
        $this->setAttribute('rss_title', 'Blog RSS');
        $this->setAttribute('posts', $entries);
        $this->setAttribute('_title', 'Index');
        
        $openid_server = WereWordConfig::get('blog.openid_server');
        if ($openid_server != '') {
            $this->setAttribute('openid_server', $openid_server);
            $openid_delegate = WereWordConfig::get('blog.openid_server_delegate');
            if ($openid_delegate != '') {
                $this->setAttribute('openid_server_delegate', $openid_delegate);
            }
        }
	}
	
	/**
	 * Reterieve posts.
	 *
	 * @return array
	 */
	protected function findPosts()
	{
        /* @var $model Public_PostsModel */
        $model = $this->context->getModel('Posts', 'Public');
        
        return $model->findBlogPosts();	    
	}
}

?>
